import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from "@src/store/base";
import { getBookingDetail } from "@src/api/booking";
import dayjs from "dayjs";
import router from "@src/router";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor'],
  }],
  data: {
    productNo: '',
    beginDay: '',
    endDay: '',
    bookingStatus: 1,
    showAllPrice: false,
    params: [],
    showParams: false,
  },
  methods: {
    onLoad(options) {
      let { productNo, beginDay, endDay, source } = options
      if (!beginDay) {
        beginDay = dayjs().format("YYYY-MM-DD");
      }
      if (!endDay) {
        endDay = null;
      }
      if (!source) {
        source = "list";
      }
      this.setData({ productNo, beginDay, endDay, source })
      this.fetchData();
    },
    //获取商品详情
    fetchData() {
      let data = {
        beginDay: this.data.beginDay,
        endDay: this.data.endDay,
        productNo: this.data.productNo
      };
      return getBookingDetail(data).then(res => {
        if (res.code === 0) {
          let productDetail = res.data;
          let bookingStatus = 1
          if (productDetail.baseInfo.productType === 0) {
            bookingStatus = productDetail.price[0].state;
          }
          let params = productDetail.attrList;
          this.handleShowParams(params)
          console.log('params', params)
          this.setData({ productDetail: res.data, bookingStatus: bookingStatus, params: params })
        }
      })
    },
    handleShowParams(params) {
      if (params && params.length > 0) {
        let flag = false;
        params.forEach(element => {
          if (element.attrValue) {
            flag = true
          }
        });
        this.setData({
          showParams: flag
        })
      } else {
        this.setData({
          showParams: false,
        })
      }
    },
    handleShowPrice() {
      this.setData({ showAllPrice: true })
    },
    handleClosePrices() {
      this.setData({ showAllPrice: false })
    },
    handleBack() {
      router.back()
    },
    handleConfirm() {
      let params = {
        productNo: this.data.productNo
      }
      if (this.data.beginDay) {
        params['beginDay'] = this.data.beginDay;
      }
      if (this.data.endDay) {
        params['endDay'] = this.data.endDay;
      }

      router.push({
        name: 'booking/submitBooking',
        params: params,
      })
    },
  }
})
